package com.fantastic.web.controller.admin;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.result.ResultData;
import com.fantastic.common.utils.JwtUtils;
import com.fantastic.model.pojo.zjs.Account;
import com.fantastic.model.pojo.zjs.User;
import com.fantastic.model.vo.zjs.UserVo;
import com.fantastic.service.zjs.AccountService;
import com.fantastic.service.zjs.AuthService;
import com.fantastic.service.zjs.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: rxx
 * @since: 2024/5/23 07:50
 * @description: 后台用户信息表请求控制器
 */
//@ResponseBody和@Controller的结合，标明本类为控制器的同时该类的全部响应都以响应体的形式进行响应
@RestController
@RequestMapping("/account")
public class AccountControllerAdmin {

    //自动注入
    @Resource
    private AccountService accountService;

    @Resource(name = "adminAuth")
    private AuthService authService;

    /**
     * 后台使用账号密码登录
     *
     * @param account
     * @return
     */
    @PostMapping("/login")
    public ResultData loginAccount(Account account) {
        UserVo user = authService.login(account);
        return new ResultData(RespStatusInfo.OK_LOGIN, user);
    }

    /**
     * 使用分页插件后，Dao层和service层返回都要用Page替换原来的list集合
     * 查询账号信息
     * 无条件时是查询全部的分页查询
     * 有条件时是查询条件的分页查询
     *
     * @param account  用户信息对象
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     * @return
     */
    @GetMapping("/listAccount")
    public ResultData listAccount(Account account, @RequestParam(name = "page",
            defaultValue = "1") int pageNum,
                                  @RequestParam(name = "limit", defaultValue = "9999999") int pageSize) {
        //设置分页逻辑
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Account> pageInfo = new PageInfo<>(accountService.listAccount(account));
        return new ResultData(RespStatusInfo.OK, pageInfo);
    }

    /**
     * 查询单个账号信息
     *
     * @param accountId
     * @return
     */
    @PostMapping("/getAccount")
    public ResultData getAccount(Integer accountId) {
        return new ResultData(RespStatusInfo.OK, accountService.getAccount(accountId));
    }

    /**
     * 修改账号信息
     *
     * @param account
     * @return
     */
    @PostMapping("/updateAccount")
    public ResultData updateAccount(Account account) {
        return new ResultData(RespStatusInfo.OK, accountService.updateAccount(account));
    }

    /**
     * 删除账号信息
     *
     * @param accountId
     * @return
     */
    @PostMapping("/deleteAccount")
    public ResultData deleteAccount(Integer accountId) {
        return new ResultData(RespStatusInfo.OK, accountService.deleteAccount(accountId));
    }


}
